//app.js
const QQMapWX = require('./resources/lib/qqmap-wx-jssdk.min')
App({
    onLaunch: function() {
        this.init()
        this.wxLocation()
    },
    init: function() {
        let userInfo = this.getUserInfo()
        this.globalData.userInfo = userInfo
        if (!userInfo) {
            wx.reLaunch({
                url: '/pages/login/index'
            })
        } else {
        }
    },
    openSetting() {
        return new Promise((resolve, reject) => {
            wx.openSetting({
                success: res => {
                    res.authSetting = {
                        'scope.userLocation': true
                    }
                    if (
                        res.errMsg == 'openSetting:ok' &&
                        res.authSetting['scope.userLocation']
                    ) {
                        resolve()
                    } else {
                        reject()
                    }
                }
            })
        })
    },
    wxLocation: function() {
        const qqmapsdk = new QQMapWX({
            key: 'FLRBZ-7QUKQ-7XF54-GBG6J-PHIU7-ZPBYJ'
        })
        let _this = this
        wx.getLocation({
            success: res => {
                wx.showLoading({
                    title: '位置获取中...'
                })
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: res.latitude,
                        longitude: res.longitude
                    },
                    success: function(res) {
                        setTimeout(() => {
                            wx.hideLoading()
                        }, 500)
                        if (res.status == 0) {
                            _this.globalData.currentAddress = res.result
                            console.log(
                                _this.globalData.currentAddress,
                                res.result
                            )
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'none',
                                duration: 1000
                            })
                        }
                    }
                })
            },
            fail: res => {
                wx.showModal({
                    title: '警告',
                    content:
                        '亲，您拒绝了微信授权，将无法正常使用小程序，请重新授权',
                    confirmText: '重新授权',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            _this
                                .openSetting()
                                .then(() => {
                                    _this.wxLocation()
                                })
                                .catch(() => {
                                    console.log('用户又特么拒绝了')
                                })
                        }
                    }
                })
            }
        })
    },
    getUserInfo: function() {
        return wx.getStorageSync('userInfo')
    },
    //实现全局监听globalData
    setWatching: function(key, method) {
        let obj = this.globalData
        //加个前缀生成隐藏变量，防止死循环发生
        let ori = obj[key] //obj[key]这个不能放在Object.defineProperty里

        if (ori) {
            //处理已经声明的变量，绑定处理
            method(ori)
        }
        Object.defineProperty(obj, key, {
            configurable: true,
            enumerable: true,
            set: function(value) {
                this['___' + key] = value
                method(value) //数据有变化的时候回调函数，实现同步功能
            },
            get: function() {
                if (typeof this['__' + key] == 'undefined') {
                    if (ori) {
                        //这里读取数据的时候隐藏变量和 globalData设置不一样，所以要做同步处理
                        this['__' + key] = ori
                        return ori
                    } else {
                        return undefined
                    }
                } else {
                    return this['__' + key]
                }
            }
        })
    },
    globalData: {
        userInfo: null,
        currentAddress: null,
        messageTypeList: [],
        merchantsTypeList: [],
        addtype: [],
        swiperList1: []
    }
})
